package com.ruibang.glass.produce.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruibang.glass.produce.constant.Constants;
import com.ruibang.glass.produce.domain.req.BucketShipmentRecordReq;
import com.ruibang.glass.produce.entity.BucketShipmentRecord;
import com.ruibang.glass.produce.entity.DefectDirectly;
import com.ruibang.glass.produce.mapper.BucketShipmentRecordMapper;
import com.ruibang.glass.produce.service.BucketShipmentRecordService;
import com.ruibang.glass.produce.util.ExcelStyleUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Author: songJinKang
 * @CreateTime: 2024-03-11  10:36
 * @Description: TODO
 * @Version: 1.0
 */

@Slf4j
@Service
public class BucketShipmentRecordServiceImpl extends ServiceImpl<BucketShipmentRecordMapper, BucketShipmentRecord> implements BucketShipmentRecordService {

    @Override
    public void deleteById(String id) {
        lambdaUpdate().eq(BucketShipmentRecord::getId, id).set(BucketShipmentRecord::getIsDelete, Constants.ONE_STR).update();
    }

    @Override
    public void edit(BucketShipmentRecord bucketShipmentRecord) {
//        lambdaUpdate().eq(BucketShipmentRecord::getId, bucketShipmentRecord.getId())
//                .set(BucketShipmentRecord::getLoadWarehouseLocation, bucketShipmentRecord.getLoadWarehouseLocation())
//                .set(BucketShipmentRecord::getUnloadWarehouseLocation, bucketShipmentRecord.getUnloadWarehouseLocation())
//                .set(BucketShipmentRecord::getAbnormalStatus, bucketShipmentRecord.getAbnormalStatus())
//                .set(BucketShipmentRecord::getAbnormalCause, bucketShipmentRecord.getAbnormalCause()).update();
        this.updateById(bucketShipmentRecord);
    }

    @Override
    public Page<BucketShipmentRecord> pageQuery(BucketShipmentRecordReq bucketShipmentRecordReq) {
        QueryWrapper<BucketShipmentRecord> qw = new QueryWrapper<>();
        qw.lambda().eq(BucketShipmentRecord::getIsDelete, Constants.ZERO_STR);
        if (bucketShipmentRecordReq.getLoadStartTime() != null && bucketShipmentRecordReq.getLoadEndTime() != null) {
            qw.lambda().between(BucketShipmentRecord::getLoadTime, bucketShipmentRecordReq.getLoadStartTime(), bucketShipmentRecordReq.getLoadEndTime());
        }
        if (bucketShipmentRecordReq.getUnloadStartTime() != null && bucketShipmentRecordReq.getUnloadEndTime() != null) {
            qw.lambda().between(BucketShipmentRecord::getUnloadTime, bucketShipmentRecordReq.getUnloadStartTime(), bucketShipmentRecordReq.getUnloadEndTime());
        }
        qw.lambda().eq(StringUtils.isNotBlank(bucketShipmentRecordReq.getContainer()), BucketShipmentRecord::getContainer, bucketShipmentRecordReq.getContainer());
        qw.lambda().eq(StringUtils.isNotBlank(bucketShipmentRecordReq.getMaterial()), BucketShipmentRecord::getMaterial, bucketShipmentRecordReq.getMaterial());
        qw.lambda().eq(StringUtils.isNotBlank(bucketShipmentRecordReq.getLoadOperator()), BucketShipmentRecord::getLoadOperator, bucketShipmentRecordReq.getLoadOperator());
        qw.lambda().orderByDesc(BucketShipmentRecord::getCreateTime);
        return this.page(new Page<>(bucketShipmentRecordReq.getPageNo(), bucketShipmentRecordReq.getPageSize()), qw.lambda());
    }

    @Override
    public void export(BucketShipmentRecordReq bucketShipmentRecordReq, HttpServletResponse response) {
        bucketShipmentRecordReq.setPageNo(-1);
        bucketShipmentRecordReq.setPageSize(-1);
        IPage<BucketShipmentRecord> page = this.pageQuery(bucketShipmentRecordReq);
        if (page != null && CollectionUtils.isNotEmpty(page.getRecords())) {
            List<BucketShipmentRecord> bucketShipmentRecordList = page.getRecords();
            try {
                EasyExcel.write(response.getOutputStream(), BucketShipmentRecord.class)
                        .registerWriteHandler(ExcelStyleUtils.createTableStyle())
                        .sheet("history_data").doWrite(bucketShipmentRecordList);
            } catch (IOException e) {
                log.error("数据导出失败：{}", e.getMessage());
            }
        }
    }
}


